// pages/activity-detail/activity-detail.js
const app = getApp();
var WxParse = require('../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    text:'', //富文本
    list:[], //更多活动
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.setData({
      id,
    })
    this.getActiveDetail(); //活动详情数据
  },

  getActiveDetail:function(){
    var that = this;
    var id = that.data.id;
    wx.request({
      url: app.globalData.host + 'active/getActiveDetail',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      data: {
        id:id,
        user_id:wx.getStorageSync('userId')
      },
      success(res) {
        wx.hideLoading();
        console.log(res)
        that.setData({
          activity:res.data.data.detail,
          text: WxParse.wxParse('text', 'html', res.data.data.detail.detail, that, 5),
          list: res.data.data.morelist.data
        })
      },
      fail(err) { },
      complete(comp) { },
    })

  },

  openActiveDetail:function(e){ //更多活动详情
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    var id = e.currentTarget.dataset.id;
    that.setData({
      id,
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
    this.getActiveDetail();
  },

  goAppyTap:function(){ //报名
    let that = this;
    let id = that.data.id;
    let params = {
      user_id: wx.getStorageSync('userId'),
      active_id: id,
    }
    app.request(app.globalData.host + 'active/activeEnroll', params, 'POST', true,
      function (res) {
        console.log(res)
        if(res.data.code == 1){
          if (that.data.activity.money*1 <= 0) {
            wx.showToast({
              title: '报名成功',
              icon: 'none'
            })
          } else {
            wx.request({
              url: app.globalData.host + 'pay/payActiveOrder',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              method: "POST",
              data: {
                orderid: res.data.data.orderid,
              },
              success(res1) {
                var res2 = res1.data
                wx.requestPayment({
                  'appId': res2.appId,
                  'timeStamp': res2.timeStamp,
                  'nonceStr': res2.nonceStr,
                  'package': res2.package,
                  'signType': res2.signType,
                  'paySign': res2.paySign,
                  'success': function (res) {
                    wx.showToast({
                      title: '支付成功',
                    })

                    wx.switchTab({
                      url: '/pages/mine/mine',
                    })
                    console.log(res);
                  },
                  'fail': function (res) {
                    wx.showToast({
                      title: '发起支付失败',
                      icon: "none"
                    })
                    console.log(res);
                  },
                  complete: function () {
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 1,
                      })
                    }, 1500)
                  }
                })
              }
            })
          }
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            confirmText: '确定',
            success: function(e) {
            },
            fail: function(res) {},
            complete: function(res) {},
          })
         
        }
      },
      function (err) { },
      function (complete) { }
    )



  },

  showTips:function(){ //已报名弹窗
    wx.showModal({
      title: '温馨提示',
      content: '您已报名！',
      showCancel: false,
      confirmText: '确定',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})